package com.sxl.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sxl.config.LayUiResult;
import com.sxl.domain.Userchecks;
import com.sxl.domain.Userroles;
import com.sxl.service.UserchecksService;
import com.sxl.service.UserrolesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ProjectName: CRMProject
 * @Package: com.sxl.controller
 * @Author songxueliang
 * @Date 2022/5/3 22:18
 */
@RequestMapping("/userchecks")
@RestController
@CrossOrigin
public class UserchecksController {
    @Autowired
    private UserchecksService userchecksService;
    @Autowired
    private UserrolesService userrolesService;
    /**
     * 修改签退
     *
     * @param
     * @return
     */
    @PostMapping("/updateUserchecks")
    public Map<String, Object> updateUserchecks(Integer userId) throws Exception {
        Date date = new Date();
        SimpleDateFormat sldf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Userchecks userchecks = userchecksService.selectAllById(userId);
        if (userchecks.getCheckState()==1){
            Date checkInTime = sldf.parse(userchecks.getCheckInTime());
            System.out.println((date.getTime()) - checkInTime.getTime());
            if ((date.getTime()) - checkInTime.getTime() < 600000) {
                  return  LayUiResult.toClient("1","十分钟之内不能签退");
            } else {
                UpdateWrapper<Userchecks> updateWrapper = new UpdateWrapper<>();
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String dqsj = df.format(new Date());
                updateWrapper.set("checkOutTime", dqsj)
                        .set("checkState", 2)
                        .eq("checkInTime", userchecks.getCheckInTime())
                        .eq("userId", userId);
                return userchecksService.update(updateWrapper) ? LayUiResult.toClient("0", "签退成功") : LayUiResult.toClient("1", "签退失败");
            }
        }
        return LayUiResult.toClient("1", "十分钟之内不能签退！！");
    }

    /**
     * 考勤签到 添加
     *
     * @param userId
     * @return
     */
    @PostMapping("/createUserchecks")
    public Map<String, Object> createUserchecks(Integer userId, String loginName) {
        Userchecks userchecks = userchecksService.selectAllById(userId);
        if (userchecks!=null){
            return LayUiResult.toClient("1", "今日已签到");
        }
        Userchecks u = new Userchecks();
        u.setUserId(userId);
        u.setUserName(loginName);
        u.setCheckState(1);
        return userchecksService.save(u) ? LayUiResult.toClient("0", "签到成功") : LayUiResult.toClient("1", "签到失败");
    }

    /**
     * 是否已签到
     * @param userId
     * @return
     */
    @PostMapping("/selectAllUserchecks")
    public Map<String,Object> selectAllUserchecks(Integer userId){
        //查询登录的用户都有什么角色
        List<Userroles> userroles = userrolesService.selectUserrolesById(userId);
        for (Userroles u : userroles) {
            if (u.getName().equals("咨询经理")){
                return LayUiResult.toClient("-1","无需签到");
            }
        }
        Userchecks userchecks = userchecksService.selectAllById(userId);
        if (userchecks==null){
            return LayUiResult.toClient("0","未签到");
        }else if(userchecks.getCheckState()==1){
            return LayUiResult.toClient("1","已签到");
        }else {
            return LayUiResult.toClient("2","已签退");
        }
    }

    /**
     * 单表页面显示
     * @param userchecks
     * @return
     */
    @GetMapping("/selectAll")
    public Map<String,Object> selectAll(Userchecks userchecks){
        List<Userchecks> list = userchecksService.selectAll(userchecks);
        int countUserchecks = userchecksService.getCountUserchecks(userchecks);
        return LayUiResult.toClient("0","成功",countUserchecks,list);
    }

    /**
     * 咨询经理帮助签退     强制无需等待时间
     * @param id
     * @return
     */
    @PostMapping("/sign")
    public Map<String,Object> sign(Integer id){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dqsj = df.format(new Date());
        UpdateWrapper<Userchecks> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("checkOutTime", dqsj)
                .set("checkState", 2)
                .eq("id", id);
        return userchecksService.update(updateWrapper) ? LayUiResult.toClient("0", "签退成功") : LayUiResult.toClient("1", "签退失败");

    }
}
